$(function() {
	$("#datagrid_list").datagrid({
		url : '/order/list.json',
		rownumbers : true,
		singleSelect : true,
		border : false,
		pagination : true,
		toolbar : '#tb',
		onClickRow : showDetails,
		columns : [ [ {
			field : "orderId",
			title : "",
			hidden : true
		}, {
			field : "orderCustId",
			title : "",
			hidden : true
		}, {
			field : "orderSubCustFid",
			title : "",
			hidden : true
		}, {
			field : "orderProductTypeFid",
			title : "",
			hidden : true
		}, {
			field : "orderNo",
			title : "订单号"
		}, {
			field : "custName",
			title : "经销商"
		}, {
			field : "orderSubCustName",
			title : "下级经销商"
		}, {
			field : "orderProductName",
			title : "保险名称"
		}, {
			field : "orderPersonCount",
			title : "被保人数"
		}, {
			field : "orderRealAmount",
			title : "订单金额",
			formatter : function(value, row, index) {
				return value.toFixed(2);
			}
		}, {
			field : "orderSettleAmount",
			title : "结算金额",
			formatter : function(value, row, index) {
				return value.toFixed(2);
			}
		}, {
			field : "orderCreatedOn",
			title : "订单时间"
		}, {
			field : "statusDesc",
			title : "订单状态"
		}, ] ],
		onLoadSuccess : function(data) {
			clearOtherGrid();
		}

	});

	$("#policy_list").datagrid({
		url : '',
		rownumbers : true,
		singleSelect : true,
		border : false,
		onClickRow : switchOperButton,
		columns : [ [ {
			field : "policyId",
			title : "",
			hidden : true
		}, {
			field : "policyStatusFid",
			title : "",
			hidden : true
		}, {
			field : "policyFeeStatusFid",
			title : "",
			hidden : true
		}, {
			field : "policyInsuredName",
			title : "姓名"
		}, {
			field : "policyInsuredCredentialType",
			title : "证件类型"
		}, {
			field : "policyInsuredCredentialNo",
			title : "证件号"
		}, {
			field : "policyInsuredBirth",
			title : "出生日期"
		}, {
			field : "policyInsuredGender",
			title : "性别"
		}, {
			field : "policyParam",
			title : " "
		}, {
			field : "policyEnd",
			title : "保险起期"
		}, {
			field : "policyEnd",
			title : "保险到期"
		}, {
			field : "policyInsuredTel",
			title : "手机号码"
		}, {
			field : "policyInsuredEmail",
			title : "电子邮箱"
		}, {
			field : "policyCount",
			title : "份数"
		}, {
			field : "policyNo",
			title : "保单号"
		}, {
			field : "statusDesc",
			title : "保单状态"
		}, ] ],
		onLoadSuccess : function(data) {
			$('#cancel_policy').attr("disabled", "disabled");
			$('#refund_policy').attr("disabled", "disabled");
		}
	});

});
function onSelectBegin(date) {
	$('obegin').val(date);
}

function onSelectEnd(date) {
	$('oend').val(date);
}

function searchit() {
	var ono = $.trim($("#ono").val());
	var pno = $.trim($("#pno").val());
	var pname = $.trim($("#pname").val());
	var cname = $.trim($("#cname").val());
	var obegin = $.trim($("input[name='obegin']").val());
	var oend = $.trim($("input[name='oend']").val());
	var ostatus = $.trim($("#ostatus").val());
	var ins = $.trim($("#ins").val());
	var otype = $.trim($("#otype").val());
	if (ono == "" && pno == "" && pname == "" && cname == "" && obegin == ""
			&& oend == "" && ostatus == "" && ins == "" && otype == "") {
		alert("请输入查询条件!");
		return;
	}
	var opts = $('#datagrid_list').datagrid("options");
	opts.url = "/order/list.json";
	$('#datagrid_list').datagrid('load', {
		"orderNo" : ono,
		"policyNo" : pno,
		"policyName" : pname,
		"custName" : cname,
		"orderBegin" : obegin,
		"orderEnd" : oend,
		"orderStatus" : ostatus,
		"insName" : ins,
		"orderType" : otype
	});
}

function showDetails() {
	var rows = $('#datagrid_list').datagrid('getRows');
	var row = $('#datagrid_list').datagrid('getSelected');
	if (rows.length == 0 || row == null) {
		return;
	}
	var oId = row.orderId;
	var oType = row.orderProductTypeFid;
	var opts = $('#policy_list').datagrid("options");
	opts.url = "/order/policy.json";
	$('#policy_list').datagrid('load', {
		'orderId' : oId,
		'orderType' : oType
	});

}

function switchOperButton() {
	var rows = $('#policy_list').datagrid('getRows');
	var row = $('#policy_list').datagrid('getSelected');
	if (rows.length == 0 || row == null) {
		$('#cancel_policy').attr("disabled", "disabled");
		$('#refund_policy').attr("disabled", "disabled");
		return;
	}
	var statusId = row.policyStatusFid;
	var feeStatusId = row.policyFeeStatusFid;
	if (statusId == 3 && feeStatusId == 2) {
		$('#cancel_policy').removeAttr("disabled");
		$('#refund_policy').attr("disabled", "disabled");
	} else if (statusId == 4 && (feeStatusId == 2 || feeStatusId == 4)) {
		$('#cancel_policy').attr("disabled", "disabled");
		$('#refund_policy').attr("disabled", "disabled");
	} else {
		$('#cancel_policy').attr("disabled", "disabled");
		$('#refund_policy').attr("disabled", "disabled");
	}
}

function clearOtherGrid() {
	$('#policy_list').datagrid('loadData', {
		total : 0,
		rows : []
	});
	switchOperButton();
}
